const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const path = require("path");
import _ from "lodash";
import m from "../models/User";
// import mEmailCode from "../models/EmailCode";
import { Request, Response } from "express";
import { NextFunction } from "express-serve-static-core";

import utils from "../utils";

import {
  pwderr,
  notuser,
  codeerr,
  user as userConfig,
  isEmail as isTypeEmail,
  isPhone as isTypePhone
} from "../config";
import * as config from "../config";
import * as cUser from "../c/user";

import { model } from "../models/User";

// import firewall from "./firewall";

router.get("/findUser", async function(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const { code, password, username } = req.query;
  const user = await m.findByUsername(username);
  let mUser = undefined;
  if (!!user) {
    mUser = utils.getDataByModel(user, model);
  }
  if (!!mUser) {
    res.json({
      code: 200,
      data: mUser,
      msg: "success"
    });
    return;
  }
  res.json({
    code: 403,
    data: undefined,
    msg: "error"
  });
});

router.post("/login", cUser.login);
router.post("/enroll", cUser.enroll);

module.exports = router;
